﻿using AutoMapper;
using D.B.Data.API.Applications.Command.RoleCommand;
using D.B.Data.Domain;
using D.B.Data.Domain.DTO.Output;
using D.B.Data.ErrorCode;
using D.B.Data.Infrastructuse;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace D.B.Data.API.Applications.CommandHandler.RoleCommandHandler
{
    public class QueryUserCommandHandler : IRequestHandler<GiveDeptIdQueryUserCommand, APIResult<List<GiveDeptIdGetUserDTO>>>
    {
        private readonly IBaseRepository<UserModel> baseRepository;
        private readonly IMapper mapper;


        public QueryUserCommandHandler(IBaseRepository<UserModel> baseRepository, IMapper mapper)
        {
            this.baseRepository = baseRepository;
            this.mapper = mapper;
        }

        public Task<APIResult<List<GiveDeptIdGetUserDTO>>> Handle(GiveDeptIdQueryUserCommand request, CancellationToken cancellationToken)
        {
           var res= baseRepository.GetAll().AsNoTracking().Where(s=>s.DeptId==request.DeptId&&!s.IsDel);

            var model = mapper.Map<List<GiveDeptIdGetUserDTO>>(res);
            APIResult<List<GiveDeptIdGetUserDTO>> aPIResult = new()
            {
                Code = EnumCode.Ok,
                Data = model,
                Msg = "成功"
            };

            return Task.FromResult(aPIResult);
            
        }
    }
}
